package org.ihelpuoo.weboo.action.http.db;

import java.util.List;

import org.ihelpuoo.lang.StringUtils;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.orm.filter.SQLFilter;
import org.ihelpuoo.util.Pager;
import org.ihelpuoo.weboo.action.http.AjaxListAction;
import org.ihelpuoo.weboo.module.ActionContext;

public class DeleteByIdsAction extends AjaxListAction
{
	@Override
	protected List generateList(ActionContext context, Pager pager) throws Exception
	{
		// get refer object
		Object refer = context.getReferObject();

		// get Ids
		String ids = StringUtils.trim(context.params().getString("ids"));
		StringBuffer sb = checkIds(ids);

		Class beanClass = context.properties().getObjectType();
		ids = sb.toString();
		ObjectRule rule = context.pojo().getRule(beanClass);
		SQLFilter filter = rule.getObjectFilterByIds(beanClass,ids,refer);
		FieldFilter ignore = context.properties().getDBIOLockedFieldFilter();

		return context.pojo().deleteBatch(beanClass,filter,ignore,refer);
	}

	private StringBuffer checkIds(String ids) throws Exception
	{
		String[] idarr = StringUtils.splitIgnoreBlank(ids);
		StringBuffer sb = new StringBuffer();
		sb.append(Integer.parseInt(idarr[0]));
		for (int i = 1; i < idarr.length; i++)
		{
			sb.append(',');
			sb.append(Integer.parseInt(idarr[i]));
		}
		return sb;
	}
}
